package io.xuan.modules.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.xuan.common.exception.LinfengException;
import io.xuan.common.utils.PageUtils;
import io.xuan.common.utils.Query;
import io.xuan.modules.app.dao.CarStationDao;
import io.xuan.modules.app.entity.CarStationEntity;
import io.xuan.modules.app.service.CarStationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service("carStationService")
public class CarStationServiceImpl extends ServiceImpl<CarStationDao, CarStationEntity> implements CarStationService {
    public List<CarStationEntity> getList() {
        List<CarStationEntity> list = this.list(); //注意！真正查询数据库的就是这个list函数！
        return list;
    }


    @Autowired
    private CarStationDao carStationDao;

    public String LineHandle(String name){
        if(name != null){
            String Lat = carStationDao.searchLat(name);
            String Lng = carStationDao.searchLng(name);
            String LngLat;
            LngLat = "lng:" + Lng + ", lat:" + Lat;
            return LngLat;
        }
        else return "null";
    }

//    public List<CarLineEntity> AllCarLine(){
//        List<CarLineEntity> carLineList = carLineDao.allLine();
//
//        for (CarLineEntity carLine : carLineList) {
//            // 查询并设置 startStation 的经纬度
//            String startLatLng = LineHandle(carLine.getStartStation());
//            carLine.setStartStation(startLatLng);
//
//            // 查询并设置 endStation 的经纬度
//            String endLatLng = LineHandle(carLine.getEndStation());
//            carLine.setEndStation(endLatLng);
//
//            String passStation1 = LineHandle(carLine.getPassStation1());
//            carLine.setPassStation1(passStation1);
//
//            String passStation2 = LineHandle(carLine.getPassStation2());
//            carLine.setPassStation2(passStation2);
//
//            String passStation3 = LineHandle(carLine.getPassStation3());
//            carLine.setPassStation3(passStation3);
//
//            String passStation4 = LineHandle(carLine.getPassStation4());
//            carLine.setPassStation4(passStation4);
//
//            String passStation5 = LineHandle(carLine.getPassStation5());
//            carLine.setPassStation5(passStation5);
//
//            String passStation6 = LineHandle(carLine.getPassStation6());
//            carLine.setPassStation6(passStation6);
//
//            String passStation7 = LineHandle(carLine.getPassStation7());
//            carLine.setPassStation7(passStation7);
//
//            String passStation8 = LineHandle(carLine.getPassStation8());
//            carLine.setPassStation8(passStation8);
//
//            String passStation9 = LineHandle(carLine.getPassStation9());
//            carLine.setPassStation9(passStation9);
//
//            String passStation10 = LineHandle(carLine.getPassStation10());
//            carLine.setPassStation10(passStation10);
//
//        }
//        return carLineList;
//    }
//
//    public List<CarLineEntity> CarLine(Integer lineId){
//        List<CarLineEntity> carLineList = carLineDao.searchLine(lineId);
//
//        for (CarLineEntity carLine : carLineList) {
//            // 查询并设置 startStation 的经纬度
//            String startLatLng = LineHandle(carLine.getStartStation());
//            carLine.setStartStation(startLatLng);
//
//            // 查询并设置 endStation 的经纬度
//            String endLatLng = LineHandle(carLine.getEndStation());
//            carLine.setEndStation(endLatLng);
//
//            String passStation1 = LineHandle(carLine.getPassStation1());
//            carLine.setPassStation1(passStation1);
//
//            String passStation2 = LineHandle(carLine.getPassStation2());
//            carLine.setPassStation2(passStation2);
//
//            String passStation3 = LineHandle(carLine.getPassStation3());
//            carLine.setPassStation3(passStation3);
//
//            String passStation4 = LineHandle(carLine.getPassStation4());
//            carLine.setPassStation4(passStation4);
//
//            String passStation5 = LineHandle(carLine.getPassStation5());
//            carLine.setPassStation5(passStation5);
//
//            String passStation6 = LineHandle(carLine.getPassStation6());
//            carLine.setPassStation6(passStation6);
//
//            String passStation7 = LineHandle(carLine.getPassStation7());
//            carLine.setPassStation7(passStation7);
//
//            String passStation8 = LineHandle(carLine.getPassStation8());
//            carLine.setPassStation8(passStation8);
//
//            String passStation9 = LineHandle(carLine.getPassStation9());
//            carLine.setPassStation9(passStation9);
//
//            String passStation10 = LineHandle(carLine.getPassStation10());
//            carLine.setPassStation10(passStation10);
//
//        }
//        return carLineList;
//    }


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CarStationEntity> page = this.page(
                new Query<CarStationEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    /**
     * 分类保存
     *
     * @param carStation
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveCarStation(CarStationEntity carStation) {
        Integer count = this.lambdaQuery()
                .eq(CarStationEntity::getStationId, carStation.getStationId())
                .count();
        if (count > 0) {
            throw new LinfengException("站点名不能重复");
        }
        boolean save = this.save(carStation);
        if (!save) {
            throw new LinfengException("站点保存失败");
        }
    }

    /**
     * 删除分类
     *
     * @param list
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByIdList(List<Integer> list) {
        // 删除分类时，需要判断该分类是否被使用

        this.removeByIds(list);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateCarStation(CarStationEntity carStation) {
        Integer count = this.lambdaQuery()
                .eq(CarStationEntity::getStationId, carStation.getStationId())
                .count();
        if (count > 1) {
            throw new LinfengException("站点名不能重复");
        }
        boolean update = this.updateById(carStation);
        if (!update) {
            throw new LinfengException("站点更新失败");
        }
    }

}
